<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use DB;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    //商品列表页
    public function goodsList(){
        $result = DB::table('goods')
        ->select('goods.*','category.cname')
        ->join('category','category.cid','=','goods.cid')
        ->Paginate(5);
        //paginate('5')->skip(0)->take(5)->get();
        //->simplePaginate(3)->get();
        //dd($result);
        // $result = ['kaka =>"afa"','ning'];

        return view('admin.goods.list',['result'=>$result]); 
    }

    //搜索商品
     public function GoodsSelect(request $request){
        
        $result=DB::table('goods')
        ->select('goods.*','category.cname')
        ->join('category','category.cid','=','goods.cid')
        ->where('goods.gname','like','%'.$request->gname.'%')
        ->Paginate(5);
        //dd($indent);
        return view('Admin.goods.list',['result'=>$result]);
    }



    //添加商品页
    public function goodsAdd(){
        //获取所有的商品分类
        $cates = DB::table('category')
        ->select(DB::raw('cid,cname,pid,level,concat(path,"-",cid) as ascPath'))
        ->orderBy('ascPath')
        ->get();
       //dd($cates); 

        return view('admin/goods/add',['cates'=>$cates]); 

    }


    //执行商品添加操作
    public function goodsInsert(Request $request)
    {

        // $name = $request->input('name');
        //判断是否有文件上传
        if(!$request->hasFile('img') || !$request->has('name') || !$request->has('cid') || !$request->has('price')){
            return back()->withInput();
        }
        
        //接收表单数据
        $result = $request->only(['name','cid','price','img']);

        //进行文件上传
        $file=$request['img'];
        $filename=uniqid().'.'.$file->getClientOriginalExtension();
        //进行文件移动       
        $file->move('./uploads/',$filename);


        //插入数据库中
        $result = DB::table('goods')->insert([
            ['gname' => $result['name'], 'cid' => $result['cid'], 'gprice' => $result['price'],'gimg' => $filename]
        ]);
        if(!$result){
            return back()->withInput();
        }
        return redirect('/admin/goodslist');
    }


    //修改商品页面
    public function goodsUpdate(Request $request, $id)
    {   
        //获取单个商品信息
        $result = DB::table('category')
        ->join('goods','category.cid','=','goods.cid')
        ->select('goods.*','category.cname','category.cid')
        ->where('goods.gid',$id)
        ->first();

        //获取所有的商品分类
        $cates = DB::table('category')
        ->select(DB::raw('cid,cname,pid,level,concat(path,"-",cid) as ascPath'))
        ->orderBy('ascPath')
        ->get();


        //dd($cates);
       
        return view('admin/goods/update',['result' => $result,'cates'=>$cates]);
    }
    //执行商品修改
    public function goodsDoUpdate(Request $request)
    {
        //获取表单提交的数据
        $result = $request->only(['name','cid','price','img','gid']);
        // dd($result);
        if($request->hasFile('img')){
            //进行文件上传
            $fileimg = $request->file('img');
            $filename = uniqid().'.'.$fileimg->getClientOriginalExtension();
            // dd($filename);
            $fileimg->move('./uploads',$filename);

            //插入数据库
            $result1 = DB::table('goods')
            ->where('gid',$result['gid'])
            ->update(
                ['gname'=>$request['name'],'cid'=>$request['cid'],'gprice'=>$request['price'],'gimg'=>$filename]
            );
            // $result1==1?return redirect('admin/goodslist');:return back()->withInput();
            if($result1){
                return redirect('admin/goodslist');
            }else{
                return back()->withInput();
            }
        }else{
            $result2 = DB::table('goods')
            ->where('gid',$result['gid'])
            ->update(
                ['gname'=>$request['name'],'cid'=>$request['cid'],'gprice'=>$request['price']]
            );

            if($result2){
                return redirect('admin/goodslist');
            }else{
                return back()->withInput();
            }
        }
    }

    //删除商品
    public function goodsDelete($gid){
      
         $result = DB::table('goods')->where('gid',$gid)->delete();
         if($result){
            return '1';
         }else{
            return '2';
        };   
    }
}
